layui.use(['layer','upload','layedit','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var delFlag = null;
    var treeSelect= layui.treeSelect;//树形下拉框
    var upload = layui.upload;
    var layedit = layui.layedit;
    var _companyId = $.cookie("companyId");
    $("#createBy").val(_companyId);
    //产品类型
    treeSelect.render({
        elem: '#tree-select-type',
        data: '/produce/category/tree/selectByCreateBy?createBy='+_companyId,
        type: 'get',
        placeholder: '顶级分类',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $("#tree-select-type").val(data.current.id);
        },
        success: function (item) { // 加载完成后的回调函数
            $("#tree-select-type").val("0");
        }
    });

    //企业列表
    $.get("/company/list",function (data) {
        $.each(data.data,function (i, ele) {
            $("#manufactorId").append('<option value="'+ ele.id +'">'+ ele.companeName +'</option>');
        });
        form.render('select');
    });

    //图片上传
    upload.render({
        elem: '#uploadImg'
        ,url: '/upload/pic'
        // ,multiple: true
        ,field:"uploadFile"
        ,before: function(obj){
        }
        ,done: function(res){
            //上传完毕
            var url = res.url;
            /*$('#showImg').html('');*/
            $('#showImg').append('<img style="padding: 10px" width="30%" src="'+ url +'" class="layui-upload-img">');
            $("#pic").val($("#pic").val() + url + ";");
        }
    });

    //创建一个编辑器
    window.editIndex = layedit.build('layedit_content',{
        height : 535,
        uploadImage : {
            url : "/layedit/upload/pic"
        }
    });

    form.verify({
        content: function(value) {
            return layedit.sync(editIndex);
        }
    });

    //用于同步编辑器内容到textarea
    layedit.sync(editIndex);

    //添加企业信息
    form.on('submit(add)', function(data){
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //初始化数据
        $.ajax({
            url: '/produce/add',
            data:data.field,
            type: 'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("添加失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("添加失败！");
            }
        });
        return false;
    });

});

